package com.hxzy.yygh.cmn.controller;

import com.hxzy.yygh.cmn.service.DictService;
import com.hxzy.yygh.common.result.Result;
import com.hxzy.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "数据字典模块")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    private DictService dictService;

    //根据数据id查询子数据列表
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id){
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    //数据字典导出
    @ApiOperation(value = "数据字典导出")
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response){
        dictService.exportData(response);
    }

    //数据字典导入
    @ApiOperation(value = "数据字典导入")
    @PostMapping("importData")
    public Result importData(MultipartFile file){
        dictService.importData(file);
        return Result.ok().message("数据导入成功!");
    }

    //根据dictCode、value查询
    @ApiOperation(value = "根据dictCode、value查询名称")
    @GetMapping("getName/{dictCode}/{value}")
    public String getDictName(@PathVariable String dictCode, @PathVariable String value){
        return dictService.getDictName(dictCode, value);
    }

    //根据value查询
    @ApiOperation(value = "根据value查询名称")
    @GetMapping("getName/{value}")
    public String getDictName(@PathVariable String value){
        return dictService.getDictName("", value);
    }

    //根据dictCode查询子节点
    @ApiOperation(value = "根据dictCode查询子节点")
    @GetMapping("getByDictCode/{dictCode}")
    public Result getByDictCode(@PathVariable String dictCode){
        List<Dict> dictChildList = dictService.getByDictCode(dictCode);
        return Result.ok(dictChildList);
    }


}
